package com.good.msgsys.controller;

import com.good.msgsys.dto.PageResult;
import com.good.msgsys.dto.Result;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.User;
import com.good.msgsys.entity.request.ChangePwdVo;
import com.good.msgsys.entity.response.UserInfo;
import com.good.msgsys.exception.CommonException;
import com.good.msgsys.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author junlong
 * @date 2020/12/31 10:18
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 新增用户
    @ApiOperation("新增用户")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result<User> add(@ApiParam(value = "新增用户信息", required = true) @RequestBody User user) throws CommonException {
        String account = user.getAccount();
        String name = user.getName();
        Integer role = user.getRole();
        User temp = userService.findByAccount(account);
        if (temp != null) {
            throw new CommonException(ResultCode.ACCOUNT_EXIST);
        }
        if (!StringUtils.isEmpty(account) && !StringUtils.isEmpty(name) && !StringUtils.isEmpty(role) && (role == 1 || role == 2)) {
            user.setCreateTime(new Date());
            user.setState(1); // 新增用户时，默认用户状态为 1 - 启用
            user.setPassword("666666"); // 初始密码
            userService.add(user);
            return new Result<>(ResultCode.SUCCESS);
        } else {
            throw new CommonException(ResultCode.PARAM_ERROR);
        }
    }

    // 根据id删除用户(逻辑删除)
    @ApiOperation("根据id删除用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result<Object> deleteById(@ApiParam(value = "用户id", required = true) @PathVariable("id") String id) {
        userService.deleteById(id);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 更新用户
    @ApiOperation("更新用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result<User> update(@ApiParam(value = "用户id", required = true) @PathVariable("id") String id, @ApiParam(value = "用户实体", required = true) @RequestBody User user) {
        user.setId(id);
        User res = userService.update(user);
        return new Result<>(ResultCode.SUCCESS, res);
    }

    // 根据id查询用户
    @ApiOperation("根据id查询用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<UserInfo> findById(@ApiParam(value = "用户id", required = true) @PathVariable("id") String id) {
        User user = userService.findById(id);
        UserInfo userInfo = new UserInfo(user);
        return new Result<>(ResultCode.SUCCESS, userInfo);
    }

    // 查询所有用户（不包括系统管理员）
    @ApiOperation("查询所有用户（不包括系统管理员）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码，从0开始", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer", defaultValue = "5")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result<PageResult<UserInfo>> findAll(@RequestParam("page") Integer page, @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        PageResult<UserInfo> userPage = userService.findAll(1, page, limit); // 默认查询所有已启用的用户，state = 1
        return new Result<>(ResultCode.SUCCESS, userPage);
    }

    // 重置密码
    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "String", required = true)
    })
    @RequestMapping(value = "/resetPwd", method = RequestMethod.GET)
    public Result<Object> resetPwd(@RequestParam("id") String id) {
        userService.resetPwd(id);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 修改密码
    @ApiOperation("修改密码")
    @RequestMapping(value = "/changePwd", method = RequestMethod.POST)
    public Result<Object> changePwd(@ApiParam(value = "修改密码实体", required = true) @RequestBody ChangePwdVo changePwdVo) throws CommonException {
        userService.changePwd(changePwdVo);
        return new Result<>(ResultCode.SUCCESS);
    }
}
